package Tracing;

import UI.View;
import prefuse.data.Table;

public class EnvironmentStack extends Table{

	public EnvironmentStack(){
		super();
		addColumn("expression", String.class);
		addColumn("state", State.class);
		addColumn("Estate", State.class);
		addColumn("Evalue", String.class);
		addColumn("Line", String.class);
	}
	
	public void push(String exp, State state, State Estate, String val, String line){
		this.addRow();
		int rowCount = this.getRowCount();
		this.setString(rowCount-1,"expression",exp);
		this.set(rowCount-1, "state", state);
		this.set(rowCount-1, "Estate", Estate);
		this.setString(rowCount-1,"Evalue",val);
		this.setString(rowCount-1,"Line",line);
	}
	
	public void pop(){
		this.removeRow(this.getRowCount()-1);
	}
	
	public void clear(){
		if(this.getRowCount()>0){
			this.pop();
		}
	}
	
	public EnvironmentStack copy(){
		EnvironmentStack copy = new EnvironmentStack();
		for(int i = 0; i < getRowCount(); i++){
			copy.addRow();
			copy.setString(i, "expression", this.getString(i, "expression"));
			copy.set(i, "state", this.get(i, "state"));
			copy.set(i, "Estate", this.get(i, "Estate"));
			copy.setString(i, "Evalue", this.getString(i, "Evalue"));
			copy.setString(i, "Line", this.getString(i, "Line"));
		}
		
		return copy;
	}
	
	public String toString(){
		StringBuffer buf = new StringBuffer();
		if(getRowCount()==0){
			buf.append( UI.Constants.empty );
			
		}
		for(int i = 0; i < getRowCount(); i++){
			/* 
			 * buf.append("\n"+i +": "+UI.Constants.zuokuohao+this.getString(i, "expression")+", "+this.getString(i, "state")+
			 	UI.Constants.compare+this.getString(i, "Estate")+", "+this.getString(i, "Evalue")+UI.Constants.youkuohao);
			 */
					buf.append("\n"+i +": "+UI.Constants.zuokuohao+this.getString(i, "expression")+", "+this.getString(i, "state")+UI.Constants.youkuohao);
		
		
		}
		
		return buf.toString();
	}
	
	public void print(){
		System.out.println("...................B-environment "+UI.Constants.benvironment+"...................");
		System.out.println(this.toString());
	}

	//print the results to view
	public void print(View view){
		view.getProcessPanel().getTextArea().setText("...................B-environment "+UI.Constants.benvironment+"..................."+this.toString());
		
	}
}
